package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Feerecord;
import com.woniuxy.caradmin.entity.vo.FeerecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.FeerecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
public class FeerecordController {
    @Autowired
    private FeerecordService feerecordService;

    @GetMapping("/feerecordList")
    @ApiOperation("查询所有的规费记录")
    public DataGridView selectAllFeeRecord(FeerecordVo feerecordVo) {
        Page<Object> page = PageHelper.startPage(feerecordVo.getPage(), feerecordVo.getLimit());
        List<Feerecord> feerecords = feerecordService.selectAllFeeRecord(feerecordVo);
        if (feerecords != null && feerecords.size() > 0) {
            return new DataGridView(page.getTotal(), feerecords);
        } else {
            throw new CarException(ResultCode.FEERECORDISNULL, Constaint.FEERECORDISNULL);
        }
    }

    @GetMapping("/addFeeRecord")
    @ApiOperation("增加规费记录")
    public R addFeeRecord(Feerecord feerecord) {
        int addFeeRecord = feerecordService.addFeeRecord(feerecord);
        if (addFeeRecord != 0) {
            return R.ok().code(ResultCode.FEERECORDISSUC).message(Constaint.FEERECORDISSUC);
        } else {
            throw new CarException(ResultCode.FEERECORDISFAI, Constaint.FEERECORDISFAI);
        }
    }

    @GetMapping("/updateFeeRecord")
    @ApiOperation("修改规费记录")
    public R updateFeeRecord(Feerecord feerecord) {
        int updateFeeRecord = feerecordService.updateFeeRecord(feerecord);
        if (updateFeeRecord != 0) {
            return R.ok().code(ResultCode.FEERECORDUPDATEISSUC).message(Constaint.FEERECORDUPDATEISSUC);
        } else {
            throw new CarException(ResultCode.FEERECORDUPDATEISFAI, Constaint.FEERECORDUPDATEISFAI);
        }
    }

    @GetMapping("/deleteFeeRecord")
    @ApiOperation("删除规费记录")
    public R deleteFeeRecord(Integer id) {
        int deleteFeeRecord = feerecordService.deleteFeeRecord(id);
        if (deleteFeeRecord != 0) {
            return R.ok().code(ResultCode.FEERECORDDDDELETEISSUC).message(Constaint.FEERECORDDDDELETEISSUC);
        } else {
            throw new CarException(ResultCode.FEERECORDDDELETEISFAI, Constaint.FEERECORDDDELETEISFAI);
        }
    }

    @GetMapping("/goFeeRecord")
    public ModelAndView goLOgin(ModelAndView modelAndView) {
        modelAndView.setViewName("feerecord");
        return modelAndView;
    }


}

